﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using QLKDS.DTO;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;

namespace QLKDS.Forms
{
    public partial class frmLevel : Form
    {
        #region Properties
        private LevelManager levelManager;
        private BarManager barManagerContext;
        private PopupMenu menuContext;

        #endregion


        #region Methods

        public frmLevel()
        {
            InitializeComponent();

            this.levelManager = new LevelManager();
            this.grvLevel.GroupPanelText = "Kéo một cột vào đây để nhóm dữ liệu";

            this.barManagerContext = new BarManager();
            this.menuContext = new PopupMenu();

            this.menuContext.ItemLinks.Add(new BarButtonItem(this.barManagerContext, "&Thêm mới"));
            this.menuContext.ItemLinks.Add(new BarButtonItem(this.barManagerContext, "&Sửa"));
            this.menuContext.ItemLinks.Add(new BarButtonItem(this.barManagerContext, "&Xóa"));
        }

        private void frmLevel_Load(object sender, EventArgs e)
        {
            // Population data on Level gridview
            this.PopulateLevelGridView();
        }

        private void frmLevel_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmMain.MainWindowManager.WindowNames.Remove(this.Name);
            frmMain.MainWindowManager.Windows.Remove(this);
        }

        public void PopulateLevelGridView()
        {
            this.Cursor = Cursors.WaitCursor;

            this.gridLevel.DataSource = this.levelManager.ListLevel();

            this.Cursor = Cursors.Default;
        }

        #endregion



        private void btnAdd_Click(object sender, EventArgs e)
        {
            var form = new frmLevelAdd();
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.PopulateLevelGridView();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int row = (int)grvLevel.FocusedRowHandle;
            var form = new frmLevelEdit();
            if (row >= 0)
            {
                int id = int.Parse((grvLevel.GetRowCellValue(row, grvLevel.Columns["Id"]) + "").ToString());

                form.Level = this.levelManager.GetLevelById(id);
            }

            if (form.ShowDialog() == DialogResult.OK)
            {
                this.PopulateLevelGridView();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int row = (int)grvLevel.FocusedRowHandle;
            if (row >= 0)
            {
                int id = int.Parse((grvLevel.GetRowCellValue(row, grvLevel.Columns["Id"]) + "").ToString());

                string name = (grvLevel.GetRowCellValue(row, grvLevel.Columns["Name"]) + "").ToString();

                if (XtraMessageBox.Show("Bạn có muốn xóa trình độ : " + name + " không?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.levelManager.DeleteLevel(id);
                    this.PopulateLevelGridView();
                    XtraMessageBox.Show("Xóa dữ liệu thành công");
                }
            }   
        }

     
    }
}
